
import com.sun.xml.internal.bind.v2.model.core.Adapter;
import java.util.ArrayList;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author dtuanh
 */
public class DictionnaryHelper {

    //determiner le type de category
    public static CategoryType getCategoryType(String line) {
        int i = line.indexOf(",");
        int j = line.indexOf(",", i + 1);
        String tmp = line.substring(i + 1, j).trim();
        if (tmp.equals("NOM")) {
            return CategoryType.NOM;

        } else if (tmp.equals("ADJ")) {
            return CategoryType.ADJ;

        }
        return CategoryType.VERB;
    }

    //lire le terme
    public static String getTerm(String line) {
        int i = line.indexOf("«");
        int j = line.indexOf("»", i + 1);
        return line.substring(i + 1, j-1).trim();
    }

    //lire liste de property
    public static List getPropertyList(String line, CategoryType type) {
        List ret = new ArrayList();
        int i = line.indexOf("<");
        int j = line.indexOf(">", i + 1);
        int n = -1;
        String property_ls = line.substring(i + 1, j).trim();

        switch (type) {
            case NOM: {
                n = property_ls.indexOf(",");
                String genre = property_ls.substring(0, n).trim();
                i = property_ls.indexOf("«", n);
                j = property_ls.indexOf("»", i + 1);
                String pluriel = property_ls.substring(i+1, j-1);

                PropertyImpl pro_genre = new PropertyImpl();
                pro_genre.setPropertyType(PropertyType.GENRE);
                if (genre.equals("MASC")) {
                    pro_genre.setGenre(CodeGenre.MASC);
                }
                if (genre.equals("FEM")) {
                    pro_genre.setGenre(CodeGenre.FEM);
                }
                ret.add(pro_genre);
                PropertyImpl pro_pluriel = new PropertyImpl();
                pro_pluriel.setPropertyType(PropertyType.PLURIEL);
                pro_pluriel.setPluriel(pluriel);
                ret.add(pro_pluriel);
                break;
            }

            case ADJ: {
                i = property_ls.indexOf("«");
                j = property_ls.indexOf("»", i + 1);
                String feminin = property_ls.substring(i+1, j-1);
                n = property_ls.indexOf(",");
                i = property_ls.indexOf("«", n);
                j = property_ls.indexOf("»", i + 1);
                String pluriel = property_ls.substring(i+1, j-1);
                PropertyImpl pro_feminin = new PropertyImpl();
                pro_feminin.setPropertyType(PropertyType.FEMININ);
                pro_feminin.setFeminin(feminin);
                ret.add(pro_feminin);
                PropertyImpl pro_pluriel = new PropertyImpl();
                pro_pluriel.setPropertyType(PropertyType.PLURIEL);
                pro_pluriel.setPluriel(pluriel);
                ret.add(pro_pluriel);
                break;

            }

            case VERB: {
                n = property_ls.indexOf(",");
                String group = property_ls.substring(0, n).trim();
                String transitive = property_ls.substring(n + 1).trim();
                //System.out.println("TRANS:"+transitive);
                PropertyImpl pro_group = new PropertyImpl();
                pro_group.setPropertyType(PropertyType.GROUPE);
                pro_group.setGroupe(Short.parseShort(group));
                ret.add(pro_group);
                PropertyImpl pro_transitive = new PropertyImpl();
                pro_transitive.setPropertyType(PropertyType.TRANSIVITE);
                if (transitive.equals("TRANS")) {
                    pro_transitive.setTrans(CodeTrans.TRANS);
                } else if (transitive.equals("INTR")) {
                    pro_transitive.setTrans(CodeTrans.INTR);
                }
                ret.add(pro_transitive);
                break;
            }

        }
        return ret;

    }

    //Lire un WORD objet
    public static Word readWord(String line) {
        WordImpl word = new WordImpl();
        CategoryImpl cate = new CategoryImpl();
        cate.setCategory(getCategoryType(line));
        word.setCategory(cate);
        word.setTerm(getTerm(line));
        List ls = getPropertyList(line, getCategoryType(line));
        for (int i = 0; i < ls.size(); i++) {
            Property tmp = (Property) ls.get(i);
            word.addProperty(tmp);
        }
        return word;
    }
//lire la definition de mot
    public static String readDefinition(String line) {
        int i = line.indexOf("«");
        int j = line.indexOf("»", i + 1);
        return line.substring(i + 1, j-1).trim();
    }
}
